package ZorkObjects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class ZorkMap
{
    ArrayList<Room> rooms;
    ArrayList<Item> globalItems;

	public ArrayList<Item> getGlobalItems() {
		return globalItems;
	}

    public ZorkMap(HashMap XMLHashMap)
    {
        rooms = new ArrayList<Room>();
        globalItems = new ArrayList<Item>();
        //HashMap extraObjects = new HashMap();

        XMLHashMap = (HashMap)XMLHashMap.get("map");
        Object roomsContainer = XMLHashMap.remove("room");
        //keySet.remove("room");

        /*
        for(String key : keySet) {
            extraObjects.put(key, XMLHashMap.get(key));
        }
        */

        if(roomsContainer instanceof ArrayList)
        {
            for(HashMap h : (ArrayList<HashMap>)roomsContainer)
            {
                Room tempRoom = new Room(h, XMLHashMap);
                rooms.add(tempRoom);
            }
        } 
        else if(roomsContainer instanceof HashMap) 
        {
            Room toAdd = new Room((HashMap)roomsContainer, XMLHashMap);
            rooms.add(toAdd);
        }
        else {
            System.out.println("this should never happen zorkmap");
        }

        Set<String> keySet = XMLHashMap.keySet();
        String[] keyArray = keySet.toArray(new String[1]);
        for(String key : keyArray)
        {
            Object container = XMLHashMap.get(key);

            if(container instanceof ArrayList)
            {
                ArrayList<HashMap> temp = (ArrayList<HashMap>)container;
                for(int i = 0; i < temp.size(); i++)
                {
                    HashMap h = temp.get(i);
                    if(isHashMapDone(h)) {
                        temp.remove(i);
                        i--;
                    }
                }

                if(((ArrayList)container).size() == 0) {
                    XMLHashMap.remove(key);
                }
            }
            else if(container instanceof HashMap)
            {
                if(isHashMapDone((HashMap)container)) {
                    XMLHashMap.remove(key);
                }
            }
        }

        ArrayList<HashMap> remainingItemsMap = (ArrayList<HashMap>)XMLHashMap.remove("item");
        if(remainingItemsMap != null) 
        {
            for(HashMap h : remainingItemsMap) {
                globalItems.add(new Item(h));
            }
        }
    }

    private boolean isHashMapDone(HashMap h)
    {
        if(h.keySet().size() == 1 &&
           h.keySet().contains("name"))
        {
            return true;
        }

        return false;
    }

    public ArrayList<Room> getRooms() {
        return rooms;
    }

    public String toString()
    {
        String toReturn = "";
        for(Room r : rooms)
        {
            toReturn += "room = \n";
            toReturn += r.toString();
            toReturn += "\n";
            //toReturn += "done printing room \n";
        }

        return toReturn;
    }
}
